'''
Created on Mar 30, 2011

@author: mwarren1
'''
import os.path
import wx
from wx.tools.dbg import Logger

import resources

import maildroplibs
import SettingsGUI
import AboutGUI

NORMAL_ICON = 0
ERROR_ICON = 1
NOCONN_ICON = 2

debug = Logger('TaskBarIcon')
debug.SetStream(maildroplibs.DEBUGSTREAM)
debug(enable = maildroplibs.ENABLEDEBUG)

def setToolTip(tooltip, icon = NORMAL_ICON):
    if TaskBarIcon._globalTaskBar is not None and TaskBarIcon._globalTaskBar.IsIconInstalled():
        debug("Changing Task Bar Icon: %s" % tooltip)
        if 'wxGTK' not in wx.PlatformInfo:
            TaskBarIcon._globalTaskBar.SetToolTip(tooltip, icon)

def showAbout(event):
    if TaskBarIcon._globalTaskBar is not None:
        debug("Showing About Page, from Outside TaskbarIcon")
        TaskBarIcon._globalTaskBar.onTaskBarAbout(event)
        
def exit():
    if TaskBarIcon._globalTaskBar is not None:
        TaskBarIcon._globalTaskBar.onTaskBarExit(None)
    else:
        wx.Exit()

class TaskBarIcon(wx.TaskBarIcon):
    '''
    classdocs
    '''
    _globalTaskBar = None

    ID_PREFS = 3248
    ID_ABOUT = 5774
    ID_EXIT = 1575
    
    def __init__(self, settings):
        '''
        Constructor
        '''
                
        wx.TaskBarIcon.__init__(self)
        
        self.__class__._globalTaskBar = self
        
        ''' Create TaskBarIcon '''
        box_icon = resources.MakeIcon(resources.getBoxIconImage())
        box_err = resources.MakeIcon(resources.getBoxErrImage())
        box_noconn = resources.MakeIcon(resources.getBoxNoConnImage())
        self.iconList = [box_icon,
                         box_err,
                         box_noconn]
        
        self.SetIcon(self.iconList[0], "MailDrop " + maildroplibs.VERSION)
 
        ''' Create Settings GUI '''
        self.settingsGUI = SettingsGUI.SettingsGUI(settings)
                
        ''' Bind Actions to Tray Icon and Menu'''
        self.Bind(wx.EVT_MENU, self.onTaskBarPrefs, id=self.ID_PREFS)
        self.Bind(wx.EVT_MENU, self.onTaskBarAbout, id=self.ID_ABOUT)
        self.Bind(wx.EVT_MENU, self.onTaskBarExit, id=self.ID_EXIT)
        if 'wxMac' in wx.PlatformInfo:
            wx.App.SetMacExitMenuItemId(self.ID_EXIT)
        
        #self.settingsGUI.Show()
        
    def CreatePopupMenu(self):
        debug('Menu Popup')
        self.menu = wx.Menu()
        self.menu.Append(self.ID_PREFS, "Settings...")
        self.menu.Append(self.ID_ABOUT, "About")
        if 'wxMac' not in wx.PlatformInfo:
            self.menu.AppendSeparator()
            self.menu.Append(self.ID_EXIT, "Exit")
        debug(str(self.menu))
        return self.menu
    
    def SetToolTip(self, tooltip, icon = NORMAL_ICON):
        self.SetIcon(self.iconList[icon], "MailDrop " + maildroplibs.VERSION + "\n"+tooltip)

    def onTaskBarPrefs(self, event):
        self.settingsGUI.Show()
        self.settingsGUI.Iconize(False)
        self.settingsGUI.Raise()
        self.settingsGUI.SetFocus()
        
    def onTaskBarAbout(self, event):
        AboutGUI.AboutGUI()
        
    def onTaskBarExit(self, event):
        self.settingsGUI.Hide()
        self.settingsGUI.OnExit(event)
        self.__class__._globalTaskBar = None
        self.RemoveIcon()
        
        maildroplibs.MailThread.stop()
        maildroplibs.connector.stop()
        maildroplibs.downloader.stop()
        
        self.Destroy()
        wx.Exit()
